package it.unipd.dei.es1011.nucularc0d3.defaultproject;

import it.unipd.dei.es1011.nucularc0d3.defaultproject.util.DataWorker;
import it.unipd.dei.es1011.nucularc0d3.defaultproject.util.Details;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
/** Void Activity Used to test Intent */
public class Detail extends TabActivity{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		TabHost tabHost = getTabHost();

		String fileName = getIntent().getExtras().getString("fileName");

		Vector<List<String>> samples = new Vector<List<String>>();
		Details details = new Details();

		DataWorker.loadSessionFromDisk(fileName, details, samples, this);
		// ----------------------------------------------------------------
		LayoutInflater.from(this).inflate(R.layout.detail, tabHost.getTabContentView(), true);

		TextView date = (TextView) findViewById(R.id.textView2);
		date.setText(details.date);
		TextView time = (TextView) findViewById(R.id.textView4);
		time.setText(details.time);
		TextView duration = (TextView) findViewById(R.id.textView6);
		duration.setText(details.duration);
		TextView sample_rate = (TextView) findViewById(R.id.textView8);
		sample_rate.setText(details.sample_rate);

		int index = 0;
		final float s_rate = Integer.parseInt(details.sample_rate) / 1000f; 
		if(details.x){
			ImageView x = (ImageView) findViewById(R.id.imageView1);
			x.setVisibility(ImageView.VISIBLE);

			List<String> list = samples.get(index);
			int n = list.size();
			List<String> lines = new ArrayList<String>(n+17);
			for(int j = 0; j < n; j++){
				String formatted = "n. " + j + "   elapsed: " + (s_rate*j) +
				"   value [m/s]: " + list.get(j);
				lines.add(formatted);
			}
			ListView x_list = (ListView) findViewById(R.id.listView1);
			x_list.setAdapter(new ArrayAdapter<String>(this, R.layout.detail_list_item, lines) {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					View row;
					LayoutInflater mInflater = LayoutInflater.from(getContext());
					if (null == convertView) {
						row = mInflater.inflate(R.layout.detail_list_item, null);
					} else {
						row = convertView;
					}

					TextView tv = (TextView) row.findViewById(android.R.id.text1);
					tv.setText(getItem(position));

					return row;
				}
			});
			index++;
		}
		if(details.y){
			ImageView y = (ImageView) findViewById(R.id.imageView2);
			y.setVisibility(ImageView.VISIBLE);

			List<String> list = samples.get(index);
			int n = list.size();
			List<String> lines = new ArrayList<String>(n+17);
			for(int j = 0; j < n; j++){
				String formatted = "n. " + j + "   elapsed: " + (s_rate*j) +
				"   value [m/s]: " + list.get(j);
				lines.add(formatted);
			}
			ListView y_list = (ListView) findViewById(R.id.listView2);
			y_list.setAdapter(new ArrayAdapter<String>(this, R.layout.detail_list_item, lines) {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					View row;
					LayoutInflater mInflater = LayoutInflater.from(getContext());
					if (null == convertView) {
						row = mInflater.inflate(R.layout.detail_list_item, null);
					} else {
						row = convertView;
					}

					TextView tv = (TextView) row.findViewById(android.R.id.text1);
					tv.setText(getItem(position));

					return row;
				}
			});
			index++;
		}
		if(details.z){
			ImageView z = (ImageView) findViewById(R.id.imageView3);
			z.setVisibility(ImageView.VISIBLE);

			List<String> list = samples.get(index);
			int n = list.size();

			List<String> lines = new ArrayList<String>(n+17);
			for(int j = 0; j < n; j++){
				String formatted = "n. " + j + "   elapsed: " + (s_rate*j) +
				"   value [m/s]: " + list.get(j);
				lines.add(formatted);
			}
			ListView z_list = (ListView) findViewById(R.id.listView3);
			z_list.setAdapter(new ArrayAdapter<String>(this, R.layout.detail_list_item, lines) {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					View row;
					LayoutInflater mInflater = LayoutInflater.from(getContext());
					if (null == convertView) {
						row = mInflater.inflate(R.layout.detail_list_item, null);
					} else {
						row = convertView;
					}

					TextView tv = (TextView) row.findViewById(android.R.id.text1);
					tv.setText(getItem(position));

					return row;
				}
			});
		}

		// ------ create upside tabs ------------------------------------
		tabHost.addTab(tabHost.newTabSpec("tab1")
				.setIndicator("", getResources().getDrawable(R.drawable.icon48x48_magnify))
				.setContent(R.id.view1));
		tabHost.addTab(tabHost.newTabSpec("tab3")
				.setIndicator("", getResources().getDrawable(R.drawable.icon48x48_x))
				.setContent(R.id.view2));
		tabHost.addTab(tabHost.newTabSpec("tab3")
				.setIndicator("", getResources().getDrawable(R.drawable.icon48x48_y))
				.setContent(R.id.view3));
		tabHost.addTab(tabHost.newTabSpec("tab4")
				.setIndicator("", getResources().getDrawable(R.drawable.icon48x48_z))
				.setContent(R.id.view4));
	}
}
